package com.xinhua.erp.service;

import com.xinhua.erp.pojo.PageResult;
import com.xinhua.erp.pojo.ProductUnit;
import com.xinhua.erp.pojo.Result;
import com.xinhua.erp.repo.ProductUnitRepo;
import com.xinhua.erp.utils.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional


public class ProductUnitService {

    @Autowired
    private ProductUnitRepo productUnitRepo;
    @Autowired
    private IdWorker idWorker;

    public Result findById(long id){

        return Result.OK(productUnitRepo.getOne(id));

    }

    public Result findAll(int page,int size){

        Pageable pageable= PageRequest.of(page - 1, size);
        Page<ProductUnit> productUnits = productUnitRepo.findAll(pageable);

        return Result.OK(new PageResult<>(productUnits.getTotalPages(),productUnits.getContent()));

    }

    public Result findAllUnit(){
            List<ProductUnit>list=productUnitRepo.findAll();
            return Result.OK(list);
    }

    public Result deleteById(long id){

        productUnitRepo.deleteById(id);

        return Result.OK();

    }

    public Result save(ProductUnit productUnit){

        productUnit.setId(idWorker.nextId());

        productUnitRepo.save(productUnit);

        return Result.OK();

    }

    public Result update(ProductUnit productUnit,Long id){

        productUnit.setId(id);

        productUnitRepo.save(productUnit);

        return Result.OK();

    }
    public Result Search(Map searchMap, int page, int size){
        Pageable pageable= PageRequest.of(page-1, size);
        Page<ProductUnit> productUnits= productUnitRepo.findAll(createSpecification(searchMap),pageable);
        return Result.OK(new PageResult<>(productUnits.getTotalElements(),productUnits.getContent()));
    }

    private Specification<ProductUnit> createSpecification(Map searchMap){
        return new Specification<ProductUnit>() {
            @Override
            public Predicate toPredicate(Root<ProductUnit> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Predicate> predicateList=new ArrayList<>();
                if(!StringUtils.isBlank((String)searchMap.get("uName"))){

                    predicateList.add(criteriaBuilder.like(root.get("uName").as(String.class),"%"+searchMap.get("uName"+"%")));

                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }



}
